package com.prim.update.utils;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.os.Environment;
import android.text.TextUtils;

import java.io.File;

/**
 * ================================================
 * 作    者：linksus
 * 版    本：1.0
 * 创建日期：11/3 0003
 * 描    述：文件工具类
 * 修订历史：
 * ================================================
 */
public class FileUtils {
    public static final String SUFFIX_APK = ".apk";
    public static final String SUFFIX_PATCH = ".patch";

    public static String sourceApkPath(Context context) {
        context = context.getApplicationContext();
        ApplicationInfo applicationInfo = context.getApplicationInfo();
        String apkPath = applicationInfo.sourceDir;
        if (TextUtils.isEmpty(apkPath)) {
            return "";
        }
        return apkPath;
    }

    /**
     * 下载文件的名字
     *
     * @param downloadUrl
     * @param versionCode
     *
     * @return
     */
    public static String getDownloadFileName(String downloadUrl, String versionCode) {
        String downloadVersion = String.valueOf(versionCode);
        int startNum = downloadUrl.lastIndexOf("/") + 1;
        String name = downloadUrl.substring(startNum, downloadUrl.length());
        if (name.endsWith(SUFFIX_APK)) {
            name = name.replace(SUFFIX_APK, "_v" + downloadVersion + SUFFIX_APK);
        } else {
            name = name.replace(SUFFIX_PATCH, "_v" + downloadVersion + SUFFIX_PATCH);
        }
        return name;
    }

    public static String getPatchDownloadFileName(String downloadUrl, String versionCode) {
        String downloadVersion = String.valueOf(versionCode);
        int startNum = downloadUrl.lastIndexOf("/") + 1;
        String name = downloadUrl.substring(startNum, downloadUrl.length());
        name = name.replace(SUFFIX_APK, "_v" + downloadVersion + SUFFIX_APK);
        return name;
    }

    public static boolean deleteFile(String fileName) {
        File file = new File(fileName);
        // 如果文件路径所对应的文件存在，并且是一个文件，则直接删除
        if (file.exists() && file.isFile()) {
            if (file.delete()) {
                System.out.println("删除单个文件" + fileName + "成功！");
                return true;
            } else {
                System.out.println("删除单个文件" + fileName + "失败！");
                return false;
            }
        } else {
            System.out.println("删除单个文件失败：" + fileName + "不存在！");
            return false;
        }
    }

    /**
     * 下载的路径
     *
     * @param path
     * @param versionCode
     *
     * @return
     */
    public static File getDownloadPath(String path, String versionCode) {
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            File file = new File(path);
            if (!file.exists()) {
                file.mkdirs();
            } else {
                // 删除当前版本的apk安装包
                String nowApkName = "_v" + String.valueOf(versionCode);
                File[] files = file.listFiles();
                for (File f : files) {
                    String fileName = f.getName();
                    if (fileName.contains(nowApkName)) {
                        f.delete();
                    }
                }
            }
            return file;
        } else {
            return null;
        }
    }

    public static File getAbsolutePath(String path, String versionCode) {
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            File file = new File(path);
            if (!file.exists()) {
                file.mkdirs();
            }
            return file;
        } else {
            return null;
        }
    }
}
